package com.example.erp;

import java.time.Duration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.client.McpSyncClient;
import io.modelcontextprotocol.client.transport.ServerParameters;
import io.modelcontextprotocol.client.transport.StdioClientTransport;
import io.modelcontextprotocol.spec.McpSchema;

/**
 * 通过bean方式注入 mcp-server-stdio 配置信息，stdio 模式配置.
 */
@Configuration
public class ClientConfig {

	@Bean(destroyMethod = "close")
	McpSyncClient mcpClient() {
		var stdioParams = ServerParameters.builder("java")
				.args("-Dspring.ai.mcp.server.stdio=true",
				"-Dspring.main.web-application-type=none", 
				"-Dlogging.pattern.console=", 
				"-jar",
				"E:\\DevSpringAi\\spring-ai-example\\ai-mcp-example\\ai-mcp-server-stdio\\target\\ai-mcp-server-stdio-0.0.1-SNAPSHOT.jar")
				.build();

		var transport = new StdioClientTransport(stdioParams);
		var mcpClient = McpClient.sync(transport)
				.requestTimeout(Duration.ofSeconds(10))
				.clientInfo(new McpSchema.Implementation("ai-mcp-client-stdio", "1.0.0"))
				.build();

		mcpClient.initialize();

		System.err.println("MCP(stdio) Initialized Ok.");

		return mcpClient;
	}
}
